package com.cmy.framework_network.environment

import android.app.Application
import android.content.Context
import android.os.Bundle
import android.os.Process
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.cmy.framework_network.R

class EnvironmentActivity : AppCompatActivity() {

    companion object {
        const val NETWORK_ENVIRONMENT_PREF_KEY = "network_environment_type"
          var sCurrentNetworkEnvironment = ""

        fun isOfficialEnvironment(context: Context): Boolean {
            val prefs = PreferenceManager.getDefaultSharedPreferences(context)
            val environment = prefs.getString(NETWORK_ENVIRONMENT_PREF_KEY, "1")
            return "1".equals(environment, ignoreCase = true)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_environment)
        //setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        //setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        supportFragmentManager.beginTransaction()
            .replace(
                R.id.content,
               MyPreferenceFragment()
            )
            .commit()
        val prefs = PreferenceManager.getDefaultSharedPreferences(this)
       sCurrentNetworkEnvironment= prefs.getString(NETWORK_ENVIRONMENT_PREF_KEY, "1")!!
    }


    override fun onBackPressed() {
        val prefs = PreferenceManager.getDefaultSharedPreferences(this)
        val newValue = prefs.getString(NETWORK_ENVIRONMENT_PREF_KEY, "1")
        if (!sCurrentNetworkEnvironment.equals(newValue, ignoreCase = true)) {
            Process.killProcess(Process.myPid())
        } else {
            finish()
        }
    }


}

